package com.xjrsoft.module.ureport.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.core.tool.utils.StringPool;
import com.xjrsoft.core.tool.utils.StringUtil;
import com.xjrsoft.module.base.entity.XjrBaseModule;
import com.xjrsoft.module.base.service.IXjrBaseModuleService;
import com.xjrsoft.module.ureport.dto.GetReleasePageListDto;
import com.xjrsoft.module.ureport.dto.ReportContentDto;
import com.xjrsoft.module.ureport.dto.ReportReleaseDto;
import com.xjrsoft.module.ureport.entity.XjrReportInfo;
import com.xjrsoft.module.ureport.entity.XjrReportRelease;
import com.xjrsoft.module.ureport.entity.XjrRptRelation;
import com.xjrsoft.module.ureport.mapper.XjrReportReleaseMapper;
import com.xjrsoft.module.ureport.service.IXjrReportReleaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xjrsoft.module.ureport.vo.ReportReleaseVo;
import com.xjrsoft.module.ureport.vo.RptRelationVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 报表菜单关联关系表 服务实现类
 * </p>
 *
 * @author zwq
 * @since 2021-04-14
 */
@Service
public class XjrReportReleaseServiceImpl extends ServiceImpl<XjrReportReleaseMapper, XjrReportRelease> implements IXjrReportReleaseService {

    @Autowired
    private IXjrBaseModuleService iXjrBaseModuleService;

    @Override
    public PageOutput<ReportReleaseVo> getPageList(GetReleasePageListDto dto) {
        QueryWrapper<XjrReportRelease> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().like(StringUtils.isNotBlank(dto.getKeyword()), XjrReportRelease::getFullName, dto.getKeyword());
        IPage<XjrReportRelease> rptRelationIPage = baseMapper.selectPage(ConventPage.getPage(dto), queryWrapper);
        PageOutput<ReportReleaseVo> pageOutput = ConventPage.getPageOutput(rptRelationIPage, ReportReleaseVo.class);
        List<ReportReleaseVo> reportReleaseVoList = pageOutput.getRows();
        //添加菜单名字，通过菜单id集合获取菜单对象集合，通过菜单对象获取菜单名字
        Set<String> parentIds = reportReleaseVoList.stream().map(ReportReleaseVo::getParentId).collect(Collectors.toSet());
        if(parentIds!=null&&parentIds.size()>0) {
            QueryWrapper<XjrBaseModule> modulequeryWrapper = new QueryWrapper();
            modulequeryWrapper.lambda().in(XjrBaseModule::getModuleId, parentIds);
            List<XjrBaseModule> xjrBaseModules = iXjrBaseModuleService.list(modulequeryWrapper);
            if (reportReleaseVoList != null && xjrBaseModules != null) {
                for (ReportReleaseVo reportReleaseVo : reportReleaseVoList) {
                    for (XjrBaseModule xjrBaseModule : xjrBaseModules) {
                        if (reportReleaseVo.getParentId().equals(xjrBaseModule.getModuleId())) {
                            reportReleaseVo.setModuleName(xjrBaseModule.getFullName());
                            break;
                        }
                    }
                }
            }
        }
        pageOutput.setRows(reportReleaseVoList);
        return pageOutput;
    }

    @Override
    public boolean saveReportRelease(ReportReleaseDto dto) throws Exception {
        //判断父级菜单是否为功能菜单
        String parentId = dto.getParentId();
        XjrBaseModule module = iXjrBaseModuleService.getById(parentId);
        if(module!=null&&"iframe".equals(module.getTarget())){
            throw new Exception("父级菜单不能为功能菜单!!!");
        }
        XjrReportRelease xjrReportRelease = BeanUtil.copy(dto, XjrReportRelease.class);
        xjrReportRelease.setId(StringUtil.randomUUID());
        XjrBaseModule savedModule = BeanUtil.copy(dto, XjrBaseModule.class);
        savedModule.setUrlAddress(StringPool.SLASH + dto.getRptFileId());
        savedModule.setTarget("iframe");
        savedModule.setIsMenu(1);
        boolean flag = iXjrBaseModuleService.save(savedModule);
        xjrReportRelease.setModuleId(savedModule.getModuleId());
        return baseMapper.insert(xjrReportRelease) > 0 &&flag;
    }

    @Override
    public boolean updateReportRelease(String id, ReportReleaseDto dto) throws Exception {
        //判断父级菜单是否为功能菜单
        String parentId = dto.getParentId();
        XjrBaseModule module = iXjrBaseModuleService.getById(parentId);
        if(module!=null&&"iframe".equals(module.getTarget())){
            throw new Exception("父级菜单不能为功能菜单!!!");
        }
        XjrReportRelease reportRelease = this.getById(id);
        String moduleId = reportRelease.getModuleId();
        XjrBaseModule xjrBaseModule = iXjrBaseModuleService.getById(moduleId);
        XjrReportRelease xjrReportRelease = BeanUtil.copy(dto, XjrReportRelease.class);
         BeanUtil.copy(dto, xjrBaseModule);
        xjrBaseModule.setUrlAddress(StringPool.SLASH + dto.getRptFileId());
        xjrReportRelease.setId(id);
        xjrBaseModule.setTarget("iframe");
        return baseMapper.updateById(xjrReportRelease) > 0&&iXjrBaseModuleService.updateById(xjrBaseModule);
    }

    @Override
    public boolean deleteReportRelease(String ids) {
        boolean flag=false;
        //分割id
        List<String> idList = Arrays.asList(ids.split(StringPool.COMMA));
        List<XjrReportRelease> xjrReportReleaseList = this.listByIds(idList);
        if (xjrReportReleaseList != null) {
            List<String> moduleIds = xjrReportReleaseList.stream().map(XjrReportRelease::getModuleId).collect(Collectors.toList());
            flag=this.removeByIds(idList)&&iXjrBaseModuleService.removeByIds(moduleIds);

        }
        return flag;
    }
}
